package com.ruitu.mall_98_2.mine;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.MD5Util;
import com.beanu.arad.utils.RegexUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.view.IdentifyingCode;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.mall_98_2.mvp.contract.RegisterContract;
import com.ruitu.mall_98_2.mvp.model.RegisterModel;
import com.ruitu.mall_98_2.mvp.presenter.RegisterPresenter;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegistActivity extends MyBaseActivity<RegisterPresenter, RegisterModel> implements RegisterContract.View {
    private EditText et_register_tel, et_register_version_code, et_register_psw, et_register_psw_agin, et_iv_verifactiion_code;
    private TextView tv_get_code, tv_agreement, tv_register;
    private CheckBox cb_agreement;
    private ImageView iv_verifactiion_code;

    private String tel;
    private String code;
    private String password;
    private String password_0;

    private int time = 60;
    private boolean isGeting = false;
    private String resultCode;
    private boolean isOpenGetCode = false;

    @Override

    protected int getLayoutRes() {
        return R.layout.activity_regist;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        tel = et_register_tel.getText().toString();
        password = et_register_psw.getText().toString();
        password_0 = et_register_psw_agin.getText().toString();
        code = et_register_version_code.getText().toString();

        if (tv_get_code == v) {
            if (!TextUtils.isEmpty(tel)) {
                if (TextUtils.equals(et_iv_verifactiion_code.getText().toString().toLowerCase(), resultCode)) {
                    tv_get_code.setEnabled(false);
                    setGetCodeTime(60);
                    isGeting = true;
                    handler.sendEmptyMessageDelayed(979, 1000);
                    getVersionCode();
                } else {
                    ToastUtils.showShort("图片验证不正确");
                }
            } else {
                ToastUtils.showShort("手机号不能为空");
            }
            getVerifactionCode();
        }

        if (tv_register == v) {
            if (!TextUtils.isEmpty(tel)) {
                if (!TextUtils.isEmpty(code)) {
                    if (RegexUtils.isPassword(password)) {
                        if (TextUtils.equals(password, password_0)) {
                            showProgress();
                            mPresenter.register(tel, MD5Util.md5String(password), code);
                        } else {
                            ToastUtils.showShort("两次输入的密码不同");
                        }
                    } else {
                        ToastUtils.showShort("密码格式不正确");
                    }
                } else {
                    ToastUtils.showShort("验证码不能为空");
                }
            } else {
                ToastUtils.showShort("手机号不能为空");
            }
        }

        if (iv_verifactiion_code == v) {
            getVerifactionCode();
        }
    }

    private void getVerifactionCode() {
        iv_verifactiion_code.setImageBitmap(IdentifyingCode.getInstance().createBitmap());
        resultCode = IdentifyingCode.getInstance().getCode().toLowerCase();
    }

    @Override
    public void setRegisterSuc() {
        finish();
    }

    @Override
    protected void initViews() {
        super.initViews();
        et_register_tel = findViewById(R.id.et_register_tel);
        et_register_version_code = findViewById(R.id.et_register_version_code);
        et_register_psw = findViewById(R.id.et_register_psw);
        et_register_psw_agin = findViewById(R.id.et_register_psw_agin);
        et_iv_verifactiion_code = findViewById(R.id.et_iv_verifactiion_code);
        tv_get_code = findViewById(R.id.tv_get_code);
        tv_agreement = findViewById(R.id.tv_agreement);
        tv_register = findViewById(R.id.tv_register);
        cb_agreement = findViewById(R.id.cb_agreement);
        iv_verifactiion_code = findViewById(R.id.iv_verifactiion_code);
        getVerifactionCode();
        setListener();
    }

    private void setListener() {
        tv_get_code.setOnClickListener(this);
        tv_agreement.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        iv_verifactiion_code.setOnClickListener(this);

        cb_agreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tv_register.setEnabled(true);
                } else {
                    tv_register.setEnabled(false);
                }
            }
        });

    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();
    }

    //获取验证码
    public void getVersionCode() {
        APIRetrofit.getDefault().smsSend(tel, "1").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort("获取验证码失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.isSuccess()) {
                            ToastUtils.showShort(baseResult.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 979) {
                if (isGeting) {
                    setGetCodeTime(--time);
                    handler.sendEmptyMessageDelayed(979, 1000);
                }
            }
        }
    };

    private void setGetCodeTime(int time) {
        if (time == 0) {
            isGeting = false;
            tv_get_code.setEnabled(true);
            tv_get_code.setText("获取验证码");
            this.time = 60;
        } else {
            tv_get_code.setText(time + "s后重新获取");
        }
    }
}
